//package net.reactor;
//
//import reactor.core.publisher.Flux;
//import reactor.core.scheduler.Schedulers;
//
//import java.util.Arrays;
//import java.util.List;
//import java.util.concurrent.CompletableFuture;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.stream.Collectors;
//
///**
// * Reactor 入门
// * 中文文档: https://htmlpreview.github.io/?https://github.com/get-set/reactor-core/blob/master-zh/src/docs/index.html#getting-started-introducing-reactor
// *
// * @author 王霄
// * @date 2021/10/1 10:22
// */
//public class Reactor01 {
//
//    public static void main(String[] args) throws InterruptedException {
//        //test1();
//        test3();
//    }
//
//    public static void test1() {
//        ExecutorService executor = Executors.newFixedThreadPool(20);
//        long start = System.currentTimeMillis();
//
//        var futureIds = CompletableFuture.supplyAsync(Reactor01::getIds, executor);
//        String join = futureIds.thenComposeAsync(ids -> {
//            var combines = ids.stream().map(id -> {
//                var future1 = CompletableFuture.supplyAsync(() -> getNameById(id), executor);
//                var future2 = CompletableFuture.supplyAsync(() -> getMoneyById(id), executor);
//                return future1.thenCombineAsync(future2, (a, b) -> String.format("%s存款%d元", a, b));
//            }).toList();
//            CompletableFuture.allOf(combines.toArray(new CompletableFuture[0])).join();
//            return CompletableFuture.supplyAsync(() -> combines.stream().map(CompletableFuture::join).collect(Collectors.joining(" ")));
//        }).join();
//
//        System.out.println(join);
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);
//
//    }
//
//    public static void test3() {
//        long start = System.currentTimeMillis();
//        //Flux<Integer> idFlux = Flux.push(sink -> {getIds().forEach(sink::next);sink.complete();});
//
//        Flux<Integer> idFlux = Flux.fromIterable(getIds());
//
//        Flux<String> nameFlux = idFlux.share()
//                .parallel(5)
//                .runOn(Schedulers.boundedElastic())
//                .map(Reactor01::getNameById)
//                .sequential();
//
//        Flux<Integer> moneyFlux = idFlux.share()
//                .parallel(5)
//                .runOn(Schedulers.boundedElastic())
//                .map(Reactor01::getMoneyById)
//                .sequential();
//
//        Flux<String> result = Flux.zip(nameFlux, moneyFlux)
//                .map(x -> String.format("%s存款%d元", x.getT1(), x.getT2()));
//
//        System.out.println(result.collectList().block());
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);
//
//    }
//
//    public static List<Integer> getIds() {
//        try {
//            Thread.sleep(5000);
//            return Arrays.asList(1, 2, 3, 4, 5);
//        } catch (InterruptedException e) {
//            throw new RuntimeException();
//        }
//    }
//
//    public static String getNameById(int id) {
//        try {
//            Thread.sleep(1000);
//            return "姓名" + id;
//        } catch (InterruptedException e) {
//            throw new RuntimeException();
//        }
//    }
//
//    public static Integer getMoneyById(int id) {
//        try {
//            Thread.sleep(1000);
//            return id * 100;
//        } catch (InterruptedException e) {
//            throw new RuntimeException();
//        }
//    }
//}
